package com.liezh.server;

import com.liezh.service.SslThriftService;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TServerSocket;

/**
 * @author liezh
 * @date 2021-9-3
 */
public class Server {
    public static void main(String[] args) {
//        bar();
        foo();
    }

    private static void bar() {
        SslThriftServiceImpl service = new SslThriftServiceImpl();
        SslThriftService.Processor<SslThriftServiceImpl> processor = new SslThriftService.Processor<>(service);

        try {
            TNonblockingServerTransport transport =
                    new TNonblockingServerSocket(9000);
            final TNonblockingServer.Args args = new TNonblockingServer.Args(transport);
            args.processor(processor);
            TServer server = new TNonblockingServer(args);

            System.out.println("Starting the simple nio server...");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void foo() {
        SslThriftServiceImpl service = new SslThriftServiceImpl();
        SslThriftService.Processor<SslThriftServiceImpl> processor = new SslThriftService.Processor<>(service);
        // 装饰器
        THeaderProcessor tHeaderProcessor = new THeaderProcessor(processor);
        try {
            TServerSocket serverTransport = new TServerSocket(9000);
            TThreadPoolServer.Args ttpsArgs = new TThreadPoolServer.Args(serverTransport);
            ttpsArgs.processor(tHeaderProcessor);
            ttpsArgs.protocolFactory(new THeaderServerProtocol.Factory());
            TServer server = new TThreadPoolServer(ttpsArgs);
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
